<template>
  <span>{{lastTime}}</span>
</template>
<script>
  import {getCallTimeLength} from '@/utils/m7Utils.js'
  export default {
    name: 'lineUpTimer',
    data () {
      return {
        lastTime: '00:00:00',
        currentTimer: null
      }
    },
    props: {
      timer: {type: String, default: ''}
    },
    watch: {
     timer: {
       immediate: true,
       handler (current) {
        if (current) {
          window.clearInterval(this.currentTimer)
          // if (this.currentTimer) { // 已经有定时器在跑，就不再新增定时器
          //   return
          // }
          let timeDiff = current ? (Math.round(Date.now()/1000).toString() - Math.round(current).toString()) : 0 // 获取当前时间与最近一次更新的时间差
          if (timeDiff >= 0) {
            this.currentTimer = setInterval(() => {
              timeDiff = timeDiff + 1
              this.lastTime = getCallTimeLength(timeDiff)
            }, 1000)
          }
        }
       }
      } 
    },
    beforeDestroy () { // 销毁的时候，清除所有tr的定时器
      window.clearInterval(this.currentTimer)
    }
  }
</script>
